﻿/*
 * Created by SharpDevelop.
 * User: 김효순
 * Date: 2012-08-29
 * Time: 오후 12:07
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace 메시지박스
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		private const int MB_OK = 0x0;
		
		[DllImport("user32.dll")]
		private static extern int MessageBoxA(IntPtr hWnd, string lpText, string lpCaption, int wType);
		
		[DllImport("user32.dll")]
		private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
		
		[DllImport("user32.dll")]
		private static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
		
		[DllImport("user32.dll")]
		private static extern int EnableWindow(IntPtr hWnd, bool fEnable);
		
		[DllImport("user32.dll")]
		private static extern int SetForegroundWindow(IntPtr hWnd);

		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void Bt1Click(object sender, EventArgs e)
		{
			string st = "test";
			
			int ret = MessageBoxA(IntPtr.Zero, st, "경고창", MB_OK);
			MessageBox.Show(ret.ToString());
		}
		
		void Bt2Click(object sender, EventArgs e)
		{
			IntPtr hMsgBox = FindWindow("#32770", "경고창");
			
			if(hMsgBox.Equals(0)) return;
			
			IntPtr hButton = FindWindowEx(hMsgBox, IntPtr.Zero, "Button", "확인");
			
			if(hButton.Equals(0)) return;
			
			if(bt2.Text.Equals("버튼비활성")){
				bt2.Text = "버튼활성";
				EnableWindow(hButton, false);
			} else if(bt2.Text.Equals("버튼활성")){
				bt2.Text = "버튼비활성";
				EnableWindow(hButton, true);
			}
		}
		
		void Timer1Tick(object sender, EventArgs e)
		{
			timer1.Enabled = false;
			IntPtr hMessageBox = FindWindow("#32770", "3초후 종료메시지");
			
			if(hMessageBox.Equals(0)) return;
			
			SetForegroundWindow(hMessageBox);
			SendKeys.Send("{ENTER}");
		}
		
		void Bt3Click(object sender, EventArgs e)
		{
			timer1.Interval = 3000;
			timer1.Enabled = true;
			
			MessageBox.Show("3초후 메세지박스가 종료됩니다", "3초후 종료메시지", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}
	}
}
